/*
 * @Author: chenxuan
 * @Date: 2022-05-18 14:40:08
 * @LastEditTime: 2022-07-22 09:13:40
 * @LastEditors: chenxuan
 */
import axios from 'axios';
import i18n from '@/i18n/i18n'
import {msg_info} from '@/assets/js/common.js'
axios.defaults.timeout = 100000;
// axios.defaults.headers.post['Content-Type'] = 'application/json; charset=utf-8';
// axios.defaults.headers.get['Content-Type'] = 'application/json; charset=utf-8';
// axios.defaults.headers.put['Content-Type'] = 'application/json; charset=utf-8'; 
// axios.defaults.headers.delete['Content-Type'] = 'application/json; charset=utf-8';
axios.defaults.crossDomain = true;
axios.defaults.withCredentials = true;
axios.defaults.headers.common['Authorization']= '';
axios.defaults.transformRequest = function(data) {
    return data;
};
// 请求拦截器
axios.interceptors.request.use(
    config => {
        if(window.sessionStorage.getItem("Authorization") && window.sessionStorage.getItem("Authorization")!=undefined){
            const Authorization = window.sessionStorage.getItem("Authorization");
            config.headers.common['Authorization']=Authorization;
        }

        return config;
    },
    error => {
        return Promise.error(error);
    }
);
axios.interceptors.response.use(
    response => {
        if (response.status === 200){
            return Promise.resolve(response);
        }
    },

    error => {
        if (error.response.status) {
            switch (error.response.status) {
                case 400:
                    msg_info(i18n.t('axios.error_Code')+"【"+"400"+"】"+error.response.data.message);
                    break;

                case 404:
                    msg_info(i18n.t('axios.error_Code')+"【"+"404"+"】"+i18n.t('axios.error_404'));
                    break;

                case 405:
                    msg_info(i18n.t('axios.error_Code')+"【"+"405"+"】"+i18n.t('axios.error_405'));
                    break;

                case 500:
                    msg_info(i18n.t('axios.error_Code')+"【"+"500"+"】"+i18n.t('axios.error_500'));
                    break;

                default:
                    msg_info(i18n.t('axios.error_Code')+"【"+error.response.status+"】"+i18n.t('axios.error_other'));
            }
            return Promise.reject(error.response);
        }
    }
);
export default axios;
